#include "PullTarget.h"

void PullTarget::AddObserver(IPullObserver& observer)
{
	_observers.push_back(&observer);
}

void PullTarget::RemoveObserver(IPullObserver& observer)
{
	std::erase_if(_observers, [&](auto it) { return it == &observer; });
}

void PullTarget::SetEvent(const Event& event)
{
	_event = event;
	Notify();
}

void PullTarget::Notify() const
{
	// 将 _observers 拷贝出来然后循环通知，可避免某个observer在OnEvent中执行RemoveObserver导致的vector迭代崩溃
	auto observers = std::vector<IPullObserver*>(_observers);
	for (auto observer : observers)
	{
		observer->OnEvent(*this);
	}
}
